<?php

namespace apiend\modules\educater\models;

use Yii;
use yii\data\ActiveDataProvider;
use common\helpers\Url;

/**
 * 教师快速检索模型
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class TeacherSearch extends \common\models\teacher\Teacher {

    //搜索
    public function search($params = array()) {
        $this->load($params, '');

        $query = static::find()
        ->where([
            'status' => static::STATUS_ACTIVE,
            'role_id' => static::ROLE_ID_TEACHER,
            'deleted_at' => 0
        ]);

        $query->andFilterWhere(['like', 'name', $this->name]);

        $query->orderBy(['grade_id' => SORT_DESC]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        Yii::$app->db->cache(function($db) use($dataProvider) {
            $dataProvider->prepare();
        }, 60);

        return $dataProvider;
    }

    public function fields() {
        return [
            'id' => function () {
                return (string) $this->id;
            },
            'name',
            'avatar' => function () {
                return Url::toRemote($this->avatar);
            },
            'grade' => function () {
                return (string) $this->grade_id;
            },
            'gender' => function () {
                return (string) $this->gender;
            },
            'nation' => function () {
                if ($this->nation) {
                    return $this->nation->name;
                }
                return '未设置';
            },
            'birthday' => function () {
                if ($this->birthday) {
                    return $this->birthday;
                }
                return '保密';
            },
            'scopes',
            'description' => function () {
                return (string) $this->description;
            },
            'certs' => 'certsImage',
            'followed',
        ];
    }

}
